package com.cl.code.database.mybatis.util;

/**
 * @author chengliang
 * @since 1.0.0
 */
public class SqlScriptUtils {

    public static final String NEWLINE = "\n";
    public static final String QUOTE = "\"";
    public static final String RIGHT_CHEV = ">";


    public static String convertIf(final String ifTest, final String sqlScript, boolean newLine) {
        String newSqlScript = sqlScript;
        if (newLine) {
            newSqlScript = NEWLINE + newSqlScript + NEWLINE;
        }
        return String.format("<if test=\"%s\">%s</if>", ifTest, newSqlScript);
    }

    public static String convertChoose(final String whenTest, final String when, final String otherwise) {
        return "<choose>" + NEWLINE
                + "<when test=\"" + whenTest + QUOTE + RIGHT_CHEV + NEWLINE
                + when + NEWLINE + "</when>" + NEWLINE
                + "<otherwise>" + otherwise + "</otherwise>" + NEWLINE
                + "</choose>";
    }

    public static String convertWhere(final String sqlScript) {
        return "<where>" + NEWLINE + sqlScript + NEWLINE + "</where>";
    }

    public static String convertSet(final String sqlScript) {
        return "<set>" + NEWLINE + sqlScript + NEWLINE + "</set>";
    }

    public static String convertBind(final String name, final String value) {
        return "<bind name=\"" + name + QUOTE + RIGHT_CHEV + value + "/>";
    }

    public static String convertTrim(String prefix, String suffix, String suffixOverrides, String sqlScript) {
        return "<trim prefix=\"" + prefix + "\" suffix=\"" + suffix + "\" suffixOverrides=\"" + suffixOverrides + "\">" +
                NEWLINE +
                sqlScript +
                NEWLINE +
                "</trim>";
    }

    public static String convertScript(final String sqlScript) {
        return "<script>" + NEWLINE + sqlScript + NEWLINE + "</script>";
    }


}
